package grafica.ventanas;

import grafica.controladores.ControladorEmpleadoBaja;
import grafica.controladores.ControladorEmpresa;
import grafica.controladores.ControladorEmpresaBaja;

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JComboBox;
import java.awt.GridBagConstraints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;

import logica.datatypes.DataEmpleado;
import logica.datatypes.DataEmpresa;

public class empleadoBaja {

	private JFrame jFrame = null;  //  @jve:decl-index=0:visual-constraint="235,70"
	private JPanel jContentPane = null;
	private JPanel jPanel = null;
	private JComboBox jComboBox = null;
	private JButton jButtonEliminar = null;
	private JButton jButtonCancelar = null;
	private ControladorEmpleadoBaja ConEmpleadoBaja;
	private DataEmpleado[] Empleados ;
	private int [] IdEmpleados;
	private JLabel jLabelMensajes = null;
	private int cant;
	private JLabel jLabel = null; 

	/**
	 * This method initializes jFrame	
	 * 	
	 * @return javax.swing.JFrame	
	 */
	public empleadoBaja(){
		ConEmpleadoBaja = new ControladorEmpleadoBaja(this);			
	}
	
	private JFrame getJFrame() {
		if (jFrame == null) {
			jFrame = new JFrame();
			jFrame.setSize(new Dimension(330, 197));
			jFrame.setTitle("Seleccione Empleado a Eliminar");
			jFrame.setContentPane(getJContentPane());
			jFrame.setLocationRelativeTo(null);	
		}
		return jFrame;
	}

	
	public void setVisible (boolean b){	
		this.getJFrame().setVisible(b);
	}
	/**
	 * This method initializes jContentPane	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getJPanel(), BorderLayout.CENTER);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			GridBagConstraints gridBagConstraints31 = new GridBagConstraints();
			gridBagConstraints31.gridx = 0;
			gridBagConstraints31.gridy = 0;
			jLabel = new JLabel();
			jLabel.setText("Empleado:");
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.gridx = 0;
			gridBagConstraints1.gridy = 2;
			jLabelMensajes = new JLabel();
			jLabelMensajes.setText("");
			jLabelMensajes.setPreferredSize(new java.awt.Dimension(200,25));
			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
			gridBagConstraints3.gridx = 1;
			gridBagConstraints3.gridy = 1;
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.gridx = 0;
			gridBagConstraints2.gridy = 1;
			
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.fill = GridBagConstraints.VERTICAL;
			gridBagConstraints.gridy = 0;
			gridBagConstraints.weightx = 1.0;
			gridBagConstraints.gridx = 1;
			gridBagConstraints.anchor = GridBagConstraints.WEST;
			
			gridBagConstraints.insets = new Insets(10,10,10,10);
			gridBagConstraints3.insets = new Insets(10,10,10,20);
			jPanel = new JPanel();
			jPanel.setLayout(new GridBagLayout());
			jPanel.add(getJComboBox(), gridBagConstraints);
			jPanel.add(getJButtonEliminar(), gridBagConstraints2);
			jPanel.add(getJButtonCancelar(), gridBagConstraints3);			
			jPanel.add(jLabelMensajes, gridBagConstraints1);
			jPanel.add(jLabel, gridBagConstraints31);
			
		}
		return jPanel;
	}

	/**
	 * This method initializes jComboBox	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getJComboBox() {
		if (jComboBox == null) {
			jComboBox = new JComboBox();	
			jComboBox.setPreferredSize(new java.awt.Dimension(160,25));
			////Completra, Cargar empresas existentes en base
			Empleados = ConEmpleadoBaja.getEmpleados();
			cant = Empleados.length;
			IdEmpleados = new int[cant];
			
			for (int i=0; i<cant; i++){	
				jComboBox.addItem(Empleados[i].getCi()+" - "+Empleados[i].getNombre()+" "+Empleados[i].getApellido());	
				IdEmpleados[i] = Empleados[i].getCi();
		   }
			
		
		}
		return jComboBox;
	}

	/**
	 * This method initializes jButtonEliminar	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonEliminar() {
		if (jButtonEliminar == null) {
			jButtonEliminar = new JButton();
			jButtonEliminar.setText("Eliminar");
			ActionListener BotonEliminar = new ActionListener(){
				public void actionPerformed (ActionEvent e){ 				
					
					ConEmpleadoBaja.EliminarEmpleado(IdEmpleados[jComboBox.getSelectedIndex()]);
					
					//Refresque de ComboBox		
					jComboBox.removeAllItems();		 
				    Empleados =  ConEmpleadoBaja.getEmpleados();				
					cant = Empleados.length;
					IdEmpleados = new int[cant];
					System.out.print(cant);
					for (int i=0; i<cant; i++){							
						  jComboBox.addItem(Empleados[i].getCi()+" - "+Empleados[i].getNombre()+" "+Empleados[i].getApellido());	
						  IdEmpleados[i] = Empleados[i].getCi();
					}
					/////////////////////////////								
				
				}
			};
			jButtonEliminar.addActionListener(BotonEliminar);
			
		}
		return jButtonEliminar;
	}

	/**
	 * This method initializes jButtonCancelar	
	 * 	
	 * @return javax.swing.JButton	
	 */
	

	private JButton getJButtonCancelar() {
		if (jButtonCancelar == null) {
			jButtonCancelar = new JButton();
			jButtonCancelar.setPreferredSize(new java.awt.Dimension(90,25));
			jButtonCancelar.setText("Cancelar");
			ActionListener BotonCancelar = new ActionListener(){
				public void actionPerformed (ActionEvent e){ 				
					jFrame.dispose();				
				}
			};
			jButtonCancelar.addActionListener(BotonCancelar );
		}
		return jButtonCancelar;
	}
	
	public void setJLabelMensajes(String info){
		this.jLabelMensajes.setText(info);
	}
	
	
}
